package com.xh.bussiness.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tiku.bean.request.PaperQuestionRequest;
import com.xh.bussiness.tiku.model.PaperQuestion;
import com.xh.bussiness.tiku.dao.PaperQuestionMapper;
import com.xh.bussiness.tiku.service.IPaperQuestionService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;

import java.util.List;

/**
 * <p>
 * 试卷试题 服务实现类
 * </p>
 *
 * @author daiwei
 * @since 2019-01-14
 */
@Service
public class PaperQuestionServiceImpl extends BaseServiceImpl<PaperQuestionMapper, PaperQuestion> implements IPaperQuestionService {
    @Override
    public XhPage queryPaperQuestionPage(XhPage page, PaperQuestionRequest bean) {
        QueryWrapper qw=new QueryWrapper<PaperQuestion>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<PaperQuestion> queryPaperQuestion(PaperQuestionRequest bean) {
        QueryWrapper<PaperQuestion> qw=new QueryWrapper<PaperQuestion>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public Integer queryQuestionScore(String paperId, String questionId) {
        PaperQuestionRequest bean = new PaperQuestionRequest();
        bean.setPaperId(paperId);
        bean.setQuestionId(questionId);
        QueryWrapper qw=new QueryWrapper<PaperQuestion>();
        //拼装条件
        qw=prepareParams(bean, qw);

        PaperQuestion paperQuestion = this.selectFirst(qw);
        if (paperQuestion == null) {
            return 0;
        }
        return paperQuestion.getScore();
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(PaperQuestionRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getPaperId())) {
            qw.eq("paper_id", bean.getPaperId());
        }
        if (!StringUtils.isEmpty(bean.getQuestionId())) {
            qw.eq("question_id", bean.getQuestionId());
        }
        return qw;
    }
}
